﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace DomainInfoLib
{
    public class SanwaysAPI : IWebAPI
    {
        private static bool? ProcessAvaliableStr(string avaliableStr)
        {
            if (string.IsNullOrEmpty(avaliableStr))
            {
                return null;
            }

            int pos = avaliableStr.IndexOf(':');

            string key = avaliableStr.Substring(pos-4, 3);

            if (key == "210")
            {
                return true;
            }

            return false;
        }


        private static string ProcessXmlStr(string XmlStr)
        {
            try
            {
                if (string.IsNullOrEmpty(XmlStr)) return "";

                return XmlStr;
            }
            catch
            {

                return "";
            }



        }


        public RegisterInfo GetResponseByWebAPI(string DomainName)
        {
            string url = "http://www.sanways.com/api/domain.asp?area_domain=" + DomainName;

            string xmlstr = BaseInfo.CallWebPage(url, null, null);

            string avaliableStr = ProcessXmlStr(xmlstr);

            bool? Avaliable = ProcessAvaliableStr(avaliableStr);

            if (!Avaliable.HasValue)
            {
                return null;
            }

            return RegisterInfo.BuildRegisterInfo(DomainName, xmlstr, Avaliable, this.GetType().Name);
        }
    }
}
